package com.example.dangerouscargodriver.ui.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.baiju.style.citylist.Toast.ToastUtils;
import com.baiju.utils.utils;
import com.example.dangerouscargodriver.R;

public class QuotedPricePopupWindow implements View.OnClickListener {
    private Context mContext = null;
    private TextView mQuotedPrice = null;
    private LinearLayout mQuotedPriceInput = null;
    private LinearLayout mQuotedPriceAdd = null;
    private TextView mQuotedPriceAdd500 = null;
    private TextView mQuotedPriceAdd1000 = null;
    private TextView mQuotedPriceAdd3000 = null;
    private TextView mQuotedPriceAddCustom = null;
    private TextView mConfirm = null;
    private PopupWindow mPopwindow = null;
    private EditText mEdQuotedPriceInput = null;

    private onInputListener mListener = null;

    private float mPrice = 0;

    private boolean mQuotedPriceCustom = false;

    public QuotedPricePopupWindow(Context context){
        mContext = context;
    }

    public void setInputListener(onInputListener listener){
        mListener = listener;
    }

    public void setPrice(float price){
        mPrice = price;
    }

    public void showPopupWindow(boolean cancelableTouchOut) {

        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View mPopview = layoutInflater.inflate(R.layout.layout_quoted_price_alert_dialog, null);

        mQuotedPrice = mPopview.findViewById(R.id.tv_quoted_price);
        if(mPrice > 0){
            mQuotedPrice.setText(String.valueOf(mPrice));
        }
        mQuotedPriceInput = mPopview.findViewById(R.id.layout_quoted_price_input);
        mQuotedPriceAdd = mPopview.findViewById(R.id.layout_quoted_price_add);
        mQuotedPriceAdd500 = mPopview.findViewById(R.id.tv_quoted_price_add_500);
        mQuotedPriceAdd500.setOnClickListener(this);
        mQuotedPriceAdd1000 = mPopview.findViewById(R.id.tv_quoted_price_add_1000);
        mQuotedPriceAdd1000.setOnClickListener(this);
        mQuotedPriceAdd3000 = mPopview.findViewById(R.id.tv_quoted_price_add_3000);
        mQuotedPriceAdd3000.setOnClickListener(this);
        mQuotedPriceAddCustom = mPopview.findViewById(R.id.tv_quoted_price_add_custom);
        mQuotedPriceAddCustom.setOnClickListener(this);
        mConfirm = mPopview.findViewById(R.id.alert_dialog_confirm);
        mConfirm.setOnClickListener(this);
        mEdQuotedPriceInput = mPopview.findViewById(R.id.ed_quoted_price_input);

        mPopwindow = new PopupWindow(mPopview, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        mPopwindow.setAnimationStyle(R.style.AnimBottom);
        mPopwindow.setBackgroundDrawable(new ColorDrawable());
        mPopwindow.setTouchable(true);
        mPopwindow.setOutsideTouchable(false);
        mPopwindow.setFocusable(true);

        mPopwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                utils.setBackgroundAlpha(mContext, 1.0f);
            }
        });

        utils.setBackgroundAlpha(mContext, 0.5f);

        mQuotedPriceCustom = false;

        mPopwindow.showAtLocation(mPopview, Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.tv_quoted_price_add_custom){
            mQuotedPriceCustom = true;
            mQuotedPriceInput.setVisibility(View.VISIBLE);
            mQuotedPriceAdd.setVisibility(View.GONE);
            mEdQuotedPriceInput.setFocusable(true);
            mEdQuotedPriceInput.setFocusableInTouchMode(true);
            mEdQuotedPriceInput.requestFocus();

        }
        else if(v.getId() == R.id.tv_quoted_price_add_500){
            mPrice += 500;
            mQuotedPrice.setText(String.valueOf(mPrice));
        }
        else if(v.getId() == R.id.tv_quoted_price_add_1000){
            mPrice += 1000;
            mQuotedPrice.setText(String.valueOf(mPrice));
        }
        else if(v.getId() == R.id.tv_quoted_price_add_3000){
            mPrice += 3000;
            mQuotedPrice.setText(String.valueOf(mPrice));
        }
        else if(v.getId() == R.id.alert_dialog_confirm){
            if(mListener != null){
                if(mQuotedPriceCustom) {
                    if (!mEdQuotedPriceInput.getText().toString().equals("")) {
                        mListener.onInput(mEdQuotedPriceInput.getText().toString());
                    }
                    else{
                        ToastUtils.showLongToast(mContext, "请输入准确的价格");
                    }
                }
                else{
                    mListener.onInput(mQuotedPrice.getText().toString());
                }
            }
            mPopwindow.dismiss();
        }
    }

    public interface onInputListener{
        void onInput(String price);
    }
}
